﻿/******************************************************************************
* File description: ExConfig.qml
* Function: 实验总流程页面
* Current Version: 1.1
* Author: 陈乃瑞
* Date: 2025-07-01
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/
import QtQuick 2.0
import "../components"
import QtCharts 2.3
import QtQuick.Layouts 1.1
import QtQuick.Controls.Styles 1.2
import QtQuick.Controls 2.12
import QtMultimedia 5.14
Rectangle {
    id: exConfig
    property var alldata: CExeConfig.getAllEx()
    property var mod11: CGetMessageInterface.getSingleMethodList(1,"")
    property var mod12: CGetMessageInterface.getSingleMethodList(2,"")
    property var mod13: CGetMessageInterface.getSingleMethodList(3,"")
    property var mod14: CGetMessageInterface.getSingleMethodList(4,"")
    property int pbflag: 3
    onVisibleChanged: {
        var value = CExeConfig.getMaskingInformation()
        if(value[0]==="1"&&value[1]==="0"){
            pbflag=1
        }else if(value[0]==="0"&&value[1]==="1"){
            pbflag=2
        }else{
            pbflag=3
        }
        refreshc()
    }

    function refreshc(){
        mod11= CGetMessageInterface.getSingleMethodList(1,"")
        mod12= CGetMessageInterface.getSingleMethodList(2,"")
        mod13= CGetMessageInterface.getSingleMethodList(3,"")
        mod14= CGetMessageInterface.getSingleMethodList(4,"")
        alldata= CExeConfig.getAllEx()
        changedMutex=false
    }
    function refreshc2(){
        refreshc()
    }

    signal refreshCombo()

    onAlldataChanged: {
        timer_allDataChanged.start()
    }
    Timer{
        id: timer_allDataChanged
        interval: 0
        onTriggered: {
            changedMutex = false
            refreshCombo()
            timer_allDataChanged.stop()
        }
    }
    Connections{  // qmlre
        target: CExeConfig
        onMaskingInformation:{
            if(value[0]==="1"&&value[1]==="0"){
                pbflag=1
            }else if(value[0]==="0"&&value[1]==="1"){
                pbflag=2
            }else{
                pbflag=3
            }
        }
    }

    property bool changedMutex: false

    Toast{
        id: toast
        y: 20*dp
        x: parent.width / 2 - 150*dp
        z: 1
    }

    CustomPopupBoxWarning{
        id: alldel
        box_text: "是否全部删除?"
        onAccepted: {
            if(changedMutex) return
            changedMutex = true
            CExeConfig.delAllEx()
            alldata=CExeConfig.getAllEx()
            changedMutex = false
        }
    }
    CustomPopupBoxWarning{
        id: saveinfo
        showRejectedButton: false
        box_text: ""
    }
    width: parent.width
    height: parent.height
    radius: 12
    color: "white"
    CustomButton{
        x:0.95*parent.width-510*dp
        y:30*dp
        defaultMode1: true
        width: 120*dp
        title: "导入"
        onReleased:{
            var data= CExeConfig.importMainExeConfig()
            if(data===0){
                toast.show("导入成功",0)
            }
            else if(data===-1){
                toast.show("文件不存在",1)
            }
            else if(data===-2){
                toast.show("导入文件数据格式不正确",1)
            }
            refreshc()
            refreshc()
        }
    }
    CustomButton{
        x:0.95*parent.width-380*dp
        y:30*dp
        defaultMode1: true
        width: 120*dp
        title: "全部保存"
        onReleased: {
            if(changedMutex) return
            changedMutex = true
            // 用于存储第二个元素的集合
            var secondElements = new Set();
            var hasDuplicate = false;
            var secondIsEmpty = false;
            var hasEmpty = false;

            for (var i = 0; i < alldata.length; i++) {
                var row = alldata[i];

                var second = row[1];
                // 检查第二个元素是否为空
                if (!second) {
                    secondIsEmpty = true;
                }
                if (secondElements.has(second)){
                    hasDuplicate = true;
                } else {
                    secondElements.add(second);
                }
                if(pbflag===1){
                    // 检查 3 - 5 元素是否为空
                    for (var j = 2; j < 5; j++) {
                        if (!row[j]) {
                            hasEmpty = true;
                            break;
                        }
                    }
                }else{
                    // 检查 3 - 6 元素是否为空
                    for (var k = 2; k < 6; k++) {
                        if (!row[k]) {
                            hasEmpty = true;
                            break;
                        }
                    }
                }
//                if (hasEmpty) {
//                    break;
//                }
            }
            if((hasDuplicate||secondIsEmpty)&&hasEmpty){
                saveinfo.box_text="保存失败，项目名称重复或未填写并且实验流程不完整！"
                saveinfo.show()
            }else if(hasDuplicate||secondIsEmpty){
                saveinfo.box_text="保存失败，项目名称重复或未填写!"
                saveinfo.show()
            }else if(hasEmpty){
                saveinfo.box_text="保存失败，实验流程不完整!"
                saveinfo.show()
            }else{
                CExeConfig.saveAllEx(alldata)
                alldata=CExeConfig.getAllEx()
                toast.show("保存成功",0)
            }
            changedMutex = false

        }
    }
    CustomButton{
        x:0.95*parent.width-250*dp
        y:30*dp
        defaultMode1: true
        width: 120*dp
        title: "新建"
        onReleased: {
            if(changedMutex) return
            changedMutex = true
            var newRow = [alldata.length+1, "", "", "", "", "", "0"];
            alldata.push(newRow)
            alldataChanged()
            changedMutex = false
        }
    }
    CustomButton{
        x:0.95*parent.width-120*dp
        y:30*dp
        defaultMode2: true
        width: 120*dp
        title: "全部删除"
        onReleased: {
            alldel.show()
        }
    }
    Rectangle{
        x:parent.width*0.05
        y:80*dp
        height: 2*dp
        width: parent.width*0.9
        color: "#DEDEDE"
    }
    Rectangle{
        x:parent.width*0.05+parent.width*0.01*0.9
        width: parent.width*0.9*0.05
        y:82*dp
        height: 58*dp
        Text {
            anchors.centerIn: parent
            text: qsTr("序号")
            color: "#666666"
            font.pixelSize: config.fontsize
        }
    }
    Rectangle{
        x:parent.width*0.05+parent.width*0.07*0.9
        width: parent.width*0.16*0.9
        y:82*dp
        height: 58*dp
        Text {
            anchors.centerIn: parent
            text: qsTr("项目名称")
            color: "#666666"
            font.pixelSize: config.fontsize
        }
    }
    Rectangle{
        x:parent.width*0.05+parent.width*0.24*0.9
        width: pbflag!==1?parent.width*0.14*0.9:parent.width*0.19*0.9
        y:82*dp
        height: 58*dp
        Text {
            anchors.centerIn: parent
            text: qsTr("1放靶")
            color: "#666666"
            font.pixelSize: config.fontsize
        }
    }
    Rectangle{
        x:pbflag!==1?parent.width*0.05+parent.width*0.39*0.9:parent.width*0.05+parent.width*0.44*0.9
        width: parent.width*0.02*0.9
        y:82*dp
        height: 58*dp
        Text {
            anchors.centerIn: parent
            text: qsTr(">")
            color: "#666666"
            font.pixelSize: config.fontsize
        }
    }
    Rectangle{
        x:pbflag!==1?parent.width*0.05+parent.width*0.42*0.9:parent.width*0.05+parent.width*0.47*0.9
        width: pbflag!==1?parent.width*0.14*0.9:parent.width*0.19*0.9
        y:82*dp
        height: 58*dp
        Text {
            anchors.centerIn: parent
            text: qsTr("2打靶")
            color: "#666666"
            font.pixelSize: config.fontsize
        }
    }
    Rectangle{
        x:pbflag!==1?parent.width*0.05+parent.width*0.57*0.9:parent.width*0.05+parent.width*0.67*0.9
        width: parent.width*0.02*0.9
        y:82*dp
        height: 58*dp
        Text {
            anchors.centerIn: parent
            text: qsTr(">")
            color: "#666666"
            font.pixelSize: config.fontsize
        }
    }
    Rectangle{
        x:pbflag!==1?parent.width*0.05+parent.width*0.6*0.9:parent.width*0.05+parent.width*0.7*0.9
        width: pbflag!==1?parent.width*0.14*0.9:parent.width*0.19*0.9
        y:82*dp
        height: 58*dp
        Text {
            anchors.centerIn: parent
            text: qsTr("3转靶")
            color: "#666666"
            font.pixelSize: config.fontsize
        }
    }
    Rectangle{
        visible: pbflag!==1
        x:parent.width*0.05+parent.width*0.75*0.9
        width: parent.width*0.02*0.9
        y:82*dp
        height: 58*dp
        Text {
            anchors.centerIn: parent
            text: qsTr(">")
            color: "#666666"
            font.pixelSize: config.fontsize
        }
    }
    Rectangle{
        visible: pbflag!==1
        x:parent.width*0.05+parent.width*0.78*0.9
        width: parent.width*0.14*0.9
        y:82*dp
        height: 58*dp
        Text {
            anchors.centerIn: parent
            text: qsTr("4纯化")
            color: "#666666"
            font.pixelSize: config.fontsize
        }
    }
    Rectangle{
        x:pbflag!==1?parent.width*0.05+parent.width*0.93*0.9:parent.width*0.05+parent.width*0.90*0.9
        width: parent.width*0.09*0.9
        y:82*dp
        height: 58*dp
        Text {
            anchors.centerIn: parent
            text: qsTr("操作")
            color: "#666666"
            font.pixelSize: config.fontsize
        }
    }
    Rectangle{
        x:parent.width*0.05
        y:140*dp
        height: 2*dp
        width: parent.width*0.9
        color: "#DEDEDE"
    }
    ScrollView {
        x:parent.width*0.05
        y:143*dp
        width: parent.width*0.9
        height: parent.height-150*dp
        clip: true
        Column{
            id: myColumnLayout
            width: exConfig.width*0.9
            height: exConfig.height-150*dp
            spacing: 10*dp
            Repeater{
                id:defmod
                model: alldata.length
                delegate: Item {
                    x:0*dp
                    width: exConfig.width*0.9
                    height: 60*dp
                    CustomExConfigList{
                        width: parent.width
                        height: parent.height
                        xh:alldata[index][0]
                        mc:alldata[index][1]
                        fbmc:alldata[index][2]
                        dbmc:alldata[index][3]
                        zbmc:alldata[index][4]
                        chmc:alldata[index][5]
                        onInfochange: {
                            if(changedMutex) return
                            changedMutex = true
                            // 触发了InfoChanged
                            alldata[index][col]=info
                            alldataChanged()
                            changedMutex = false
                        }
                        onBt3: {        // 删除
//                            if(changedMutex) return
//                            changedMutex = true
//                            alldata.splice(index, 1);
//                            // 遍历二维数组
//                            for (var i = 0; i < alldata.length; i++) {
//                                alldata[i][0] = i + 1;
//                            }
//                            alldataChanged()
//                            changedMutex = false
                            if(changedMutex) return
                            changedMutex = true
                            CExeConfig.delExbyid(index+1)
                            alldata=CExeConfig.getAllEx()
                            changedMutex = false
                        }
                    }
                }
            }
            Text {
                x:0*dp
                font.pixelSize: config.fontsize
                text: qsTr("注:实验流程必须全部添加")
            }
        }
    }
}
